package com.example.controller;

import com.example.common.Result;
import com.example.entity.User;
import com.example.entity.UserVO;
import com.example.service.AdminService;
import com.example.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class WebController{
    @Resource
    private AdminService adminService;
    @Resource
    private UserService userService;
    /**
     * 默认请求接口
     */
    @GetMapping("/")
    public Result hello() {
        return Result.success();
    }

    /**
     *
     * 登录接口
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user ){
        UserVO user1 ;
        if(user.getRole().equals("管理员"))
        {
            user1 = adminService.login(user); //管理员登录
        } else if(user.getRole().equals("普通用户"))
        {
            user1 = userService.login(user); //学生登录
        }
        else{
            return  Result.error("账号或密码错误");
        }

        return Result.success(user1);
    }
    /**
     * 注册接口
     */
    @PostMapping("/register")
    public Result register(@RequestBody User user){

        if(user.getRole().equals("普通用户"))
            userService.register(user);
        else if(user.getRole().equals("管理员"))
            adminService.register(user);
        else
            return Result.error("请选择用户角色");
        //管理员不能自己注册账号，只能管理员注册
        // adminService.register(account);
        return Result.success();
    }
}

